package bookStore.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookStore.po.Author;
import bookStore.po.Book;
import bookStore.service.business.AuthorService;
import bookStore.service.business.AuthorServiceImpl;
import bookStore.service.business.BookService;
import bookStore.service.business.BookServiceImpl;
import bookStore.tool.Constants;
import bookStore.tool.Messages;
import bookStore.tool.ParameterMapUtil;
import bookStore.tool.UploadTool;
import bookStore.tool.UrlUtil;
import bookStore.vo.BookVo;
import bookStore.vo.BookWithAuthorName;
import cn.hutool.core.bean.BeanUtil;

/**
 * Servlet implementation class BookManagerController
 */
@WebServlet("/bookManager/*")
@MultipartConfig
public class BookManagerController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String url;
	private String traget = "";
	private BookService bookService = new BookServiceImpl();
	private AuthorService authorService = new AuthorServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BookManagerController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		url = UrlUtil.getUrl(request.getRequestURI());
		Map<String, String> parametersMap;
		Book book;
		String offset=request.getParameter("pager.offset");
		String pageSize=request.getParameter("pageSize");
		switch (url) {
		case "list":
			List<BookVo> books = bookService.queryAllBookVo(Integer.parseInt(offset),Integer.parseInt(pageSize));
			request.setAttribute("books", books);
			request.setAttribute("total", bookService.count());
			setTraget("/WEB-INF/manager/book/list.jsp");
			break;
		//跳转到添加图书页面
		case "toSaveForm":
			String authorId=request.getParameter("authorId");
			if(authorId!=null) {
				String authorName=authorService.getAuthorNameById(Integer.parseInt(authorId));
				request.getSession().setAttribute("authorName", authorName);
			}
			setTraget("/WEB-INF/manager/book/form.jsp");
			break;
		//添加图书时打开作者列表以便选择
		//传一个名为type的变量区分是哪一个表单的动作 新增or修改 toSaveForm or get	
		case "toAuthorList":
			List<Author> authors = authorService.queryAll(0, Constants.PAGE_SIZE);
			request.setAttribute("authors", authors);
			request.setAttribute("type", request.getParameter("type"));
			setTraget("/WEB-INF/manager/book/authorList.jsp");
			break;
		//选择作者完成，返回添加图书表单,附带作者id
		case "getAuthor":
			String authorId2 = request.getParameter("authorId");
			String type=request.getParameter("type");
			String bookId = request.getParameter("bookId");
			System.out.println("getAuthor:authorId="+authorId2);
			if(type.equals("get")) {
				setTraget(type+"?bookId="+bookId+"&authorId="+authorId2);
			}else {
				setTraget(type+"?authorId="+authorId2);
			}
			break;
		case "save":
			parametersMap = ParameterMapUtil.createParameterMapFormRequest(request);
			String pictureName = UploadTool.uploadFile(Constants.PICTURES_HOME, Constants.PICTURE_FILE_NAME, request);
			book = BeanUtil.fillBeanWithMap(parametersMap, new Book(), false);
			book.setPicture(Constants.PICTURES_PATH+pictureName);
			bookService.save(book);
			request.setAttribute("message", Messages.SUCCESS_FOR_SAVE);
			setTraget("list?pager.offset=0&pageSize="+Constants.PAGE_SIZE);
			break;
		case "get":
			int bookId1 = Integer.parseInt(request.getParameter("bookId"));
			BookWithAuthorName book1 = bookService.getBookWithAuthorName(bookId1);
			String authorId1=request.getParameter("authorId");
			if(authorId1!=null) {
				String authorName=authorService.getAuthorNameById(Integer.parseInt(authorId1));
				book1.setAuthorId(Integer.parseInt(authorId1));
				book1.setAuthorName(authorName);				
			}
			request.setAttribute("book", book1);
			setTraget("/WEB-INF/manager/book/item.jsp");
			break;
		case "update":
			parametersMap = ParameterMapUtil.createParameterMapFormRequest(request);
			String updatePictureName = UploadTool.uploadFile(Constants.PICTURES_HOME, "pictureFile", request);
			book = BeanUtil.fillBeanWithMap(parametersMap, new Book(), false);
			book.setPicture(Constants.PICTURES_PATH+updatePictureName);
			bookService.update(book);
			request.setAttribute("message", Messages.SUCCESS_ACTION);
			setTraget("get?bookId="+book.getId()+"&authorName="+parametersMap.get("authorName"));
			break;
		case "delete":
			int bookId2 =Integer.parseInt(request.getParameter("bookId"));
			bookService.delete(bookId2);
			request.setAttribute("message", Messages.SUCCESS_ACTION);
			setTraget("list?pager.offset=0&pageSize="+Constants.PAGE_SIZE);
			break;
		}
		

		if(traget.endsWith(".jsp")) {
			request.getRequestDispatcher(traget).forward(request, response);
		}else {
			response.sendRedirect(traget);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	public String getTraget() {
		return traget;
	}

	public void setTraget(String traget) {
		this.traget = traget;
	}

}
